<?php

class LibraryHelper {
  static public function load($library, &$attached = NULL) {
    $_libraries = is_array($library) ? $library : array(array('name' => $library));
    $library_path = 'sites/all/libraries/bower_components';

    $libraries = array(
      'masonry' => array(
        'js' => array($library_path . '/masonry/dist/masonry.pkgd.min.js'),
      ),
      'underscore' => array(
        'js' => array($library_path . '/underscore/underscore-min.js'),
      ),
      'jquery-infinite-scroll' => array(
        'js' => array($library_path . '/jquery-infinite-scroll/jquery.infinitescroll.min.js'),
      ),
      'imagesloaded' => array(
        'js' => array($library_path . '/imagesloaded/imagesloaded.pkgd.min.js'),
      ),
      'salvattore' => array(
        'js' => array($library_path . '/salvattore/dist/salvattore.min.js'),
      ),
      'jquery.autogrow-textarea' => array(
        'js' => array('sites/all/libraries/jquery.autogrow-textarea.js'),
      ),
      'jquery-timeago' => array(
        'js' => array($library_path . '/jquery-timeago/jquery.timeago.js'),
      ),
      'angular' => array(
        'js' => array($library_path . '/angular/angular.min.js'),
      ),
      'angular-sanitize' => array(
        'js' => array($library_path . '/angular-sanitize/angular-sanitize.min.js'),
      ),
      'angular-file-upload' => array(
        'js' => array($library_path . '/angular-file-upload/angular-file-upload.min.js'),
      ),
      'angular-utils-pagination' => array(
        'js' => array($library_path . '/angular-utils-pagination/dirPagination.js'),
      ),
    );

    foreach($_libraries as $library) {
      // add js
      if(isset($libraries[$library['name']]['js']) && !empty($libraries[$library['name']]['js'])) {
        foreach($libraries[$library['name']]['js'] as $js) {
          if(empty($attached)) {
            drupal_add_js($js, isset($library['options']) ? $library['options'] : NULL);
          }
          else {
            $attached['js'][] = $js;
          }
        }
      }

      // add css
      if(isset($libraries[$library['name']]['css']) && !empty($libraries[$library['name']]['css'])) {
        foreach($libraries[$library['name']]['css'] as $css) {
          if(empty($attached)) {
            drupal_add_css($css, isset($library['options']) ? $library['options'] : NULL);
          }
          else {
            $attached['css'][] = $css;
          }
        }
      }
    }
  }
}
